#ifndef MSCOMMON_INNER_C
#define MSCOMMON_INNER_C
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <libmslog/mslog.h>
#include "mscommon.h"
#include "mstime.h"
#include "msstring.h"
#include "msmd.h"

#define FLAG "MSCOMMON_INNER"

#define mscmd_file	"/tmp/mscmd_info"

ms_bool mst_innerapi_cmdRet(ms_string ms_in cmd)  
{  
	ms_bufcheckRetDes (-1, cmd, "Error param ###cmd");
	ms_strcat(cmd,"  > /dev/null");
	ms_u32 status = system(cmd);  
	if (0 == status){
#if defined(OS_LINUX_SOC)
		if (WIFEXITED(status)&&(0 == WEXITSTATUS(status)))  {  
			return ms_true;
		}
#else
		return ms_true;
#endif
	}
	return ms_false;
}  
ms_s32 mst_innerapi_cmdReadByPipe( ms_string ms_in cmd,ms_string ms_out info ,ms_u32 ms_in maxlen)  
{  
	ms_pamcheckRet(-1, cmd, "cmd");
	ms_pamcheckRet(-1, info, "info");
	ms_memset(info, 0, maxlen);
	
	FILE *pfp=popen(cmd, "r"); //打开PIPE进程
	if(ms_null==pfp){
		return -1;
	}
	ms_s32 len= fread(info, sizeof(char), maxlen, pfp);  
	pclose(pfp);
	return len;
}  

ms_s32 mst_innerapi_cmdReadByTmp( ms_string ms_in cmd,ms_string ms_out info ,ms_u32 ms_in maxlen)  {  
	ms_pamcheckRet(-1, cmd, "cmd");
	ms_pamcheckRet(-1, info, "info");
	char cmdinfo[1024]={0};
	
	ms_cmd(cmdinfo,"%s >%s", cmd,mscmd_file);//将指令执行结果写入临时文件
	return msmd_api_read(mscmd_file,info,maxlen);
}  

ms_bool mst_innerapi_cmdIsGrep( ms_string ms_in cmd )  
{  
	ms_pamcheckRet(-1, cmd, "cmd");
	ms_array buf[2048]={0};  
	int len=mst_innerapi_cmdReadByPipe(cmd, (ms_string)buf, (sizeof(buf)-1));
	return (len > 0) ? ms_true : ms_false;  
}  

ms_void mst_innerapi_kill( ms_string ms_in cmd )  
{  
	ms_pamcheck( cmd, "cmd");
	if(ms_true==mst_innerapi_cmdIsGrep(cmd)){
		ms_byte tempbuf[1024]={0};
		ms_cmd(tempbuf,"kill -9 ` %s`",cmd);
	}
}  
ms_s08 mst_innerapi_bufSwitch(ms_byte * ms_io buf1,ms_pbyte ms_io buf2 ,ms_u32 ms_in len)  
{  
	ms_pamcheckRet(-1, buf1, "buf1");
	ms_pamcheckRet(-1, buf2, "buf2");

	ms_byte tmpbuf[1024]={0};
	if(len>ms_bufsize(tmpbuf)){
		return -2;
	}	

	ms_memcpy(tmpbuf,buf1,len);
	ms_memcpy(buf1,buf2,len);
	ms_memcpy(buf2,tmpbuf,len);
	return 0;
}  

ms_s08 mst_innerapi_bufOffset(ms_byte * ms_io buf,ms_s32 ms_in offset_byte,ms_u32 ms_in len)  
{  	
	ms_pamcheckRet(-1, buf, "buf");
	ms_byte * pbuf=ms_null;
	if(offset_byte>0){
		/*offset_byte为3，len为4
			处理前数据：data:1,2,3,4,5,6,7
			处理后数据：data:4,5,6,7,0,0,0*/
		ms_malloc_retErr(-1,pbuf, len, "selfcp_buf");
		ms_memcpy(pbuf,&buf[offset_byte],len);
		ms_memset(buf, 0, (len+offset_byte));
		ms_memcpy(buf,pbuf,len);
	}else{
		/*offset_byte为-3，len为7
			处理前数据：data:1,2,3,4,5,6,7
			处理后数据：data:0,0,0,1,2,3,4,5,6,7*/
		ms_malloc_retErr(-1,pbuf, (len+(-offset_byte)), "selfcp_buf");
		ms_memcpy(&pbuf[(-offset_byte)],buf,len);
		ms_memset(buf, 0, (len+(-offset_byte)));
		ms_memcpy(buf,pbuf,(len+(-offset_byte)));
	}
	ms_deMalloc(pbuf);
	return 0;
}  
ms_void mst_innerapi_getOutName(ms_string ms_in url,ms_string ms_in vodpath,ms_string ms_in formatInfo,ms_string ms_out programname)
{
	ms_pamcheck(url, "url");
	ms_pamcheck(vodpath, "vodpath");
	ms_pamcheck(programname, "programname");

	if( ms_moreThan(ms_buflen(url),(ms_buflen(formatInfo)))&&ms_strncmp_sneq((url), vodpath)	){
		ms_sprintfs(programname, "%s",url);
		int len=ms_buflen(programname);
		memset(&programname[len-ms_buflen(formatInfo)],0,ms_buflen(formatInfo));
	}
}
ms_bool mst_innerapi_getOutPath(ms_string ms_in url,ms_string ms_in segPath,ms_string ms_in formatInfo,ms_string ms_out outPath)
{
	//url=test.m3u8,segPath=/voddir/,formatInfo=".m3u8",outPath=/voddir/test
	if( ms_moreThan(ms_buflen(url),(ms_buflen(formatInfo)))&&ms_strncmp_sneq((url), segPath) ){
		ms_sprintfs(outPath, "%s/%s", segPath,url);
		int len=ms_buflen(outPath);		
		ms_memset0(&outPath[len-ms_buflen(formatInfo)],ms_buflen(formatInfo));
		ms_debug("-------outPath:%s",outPath);
		char cmdbuf[512]={0};
		ms_cmd(cmdbuf, "rm -fr %s", outPath);
		ms_cmd(cmdbuf, "mkdir  -p %s  && chmod 777 %s", outPath, outPath);
		return ms_true;
	}
	return ms_false;
}

ms_string mst_innerapi_bit2str_64( ms_string ms_out outstr, ms_u64 byte,ms_bool falg_bit)
{
	ms_pamcheckRet(ms_null, outstr, "outstr");
	if(ms_false==falg_bit){
		if(ms_noLess(byte, ms_GB)){ 
			snprintf(outstr, 32, "%s", ms_B2GB64_unit(byte));
		} else if(ms_noLess(byte, ms_MB)){
			snprintf(outstr, 32, "%s", ms_B2MB64_unit(byte));
		} else if(ms_noLess(byte, ms_KB)){
			snprintf(outstr, 32, "%s", ms_B2KB64_unit(byte));
		} else{
			snprintf(outstr, 32, "%s", ms_B64_unit(byte));
		}
	}else{
		if(ms_noLess(byte, ms_gb)){ 
			snprintf(outstr, 32, "%s", ms_b2gb64_unit(byte));
		} else if(ms_noLess(byte, ms_mb)){
			snprintf(outstr, 32, "%s", ms_b2mb64_unit(byte));
		} else if(ms_noLess(byte, ms_kb)){
			snprintf(outstr, 32, "%s", ms_b2kb64_unit(byte));
		} else{
			snprintf(outstr, 32, "%s", ms_b64_unit(byte));
		}
	}
	return outstr;
}
ms_string mst_innerapi_bit2str_32( ms_string ms_out outstr, ms_u32 byte,ms_bool falg_bit)
{
	ms_pamcheckRet(ms_null, outstr, "outstr");
	if(ms_false==falg_bit){
		if(ms_noLess(byte, ms_GB)){ 
			snprintf(outstr, 32, "%s", ms_B2GB32_unit(byte));
		} else if(ms_noLess(byte, ms_MB)){
			snprintf(outstr, 32, "%s", ms_B2MB32_unit(byte));
		} else if(ms_noLess(byte, ms_KB)){
			snprintf(outstr, 32, "%s", ms_B2KB32_unit(byte));
		} else{
			snprintf(outstr, 32, "%s", ms_B32_unit(byte));
		}
	}else{
		if(ms_noLess(byte, ms_gb)){ 
			snprintf(outstr, 32, "%s", ms_b2gb32_unit(byte));
		} else if(ms_noLess(byte, ms_mb)){
			snprintf(outstr, 32, "%s", ms_b2mb32_unit(byte));
		} else if(ms_noLess(byte, ms_kb)){
			snprintf(outstr, 32, "%s", ms_b2kb32_unit(byte));
		} else{
			snprintf(outstr, 32, "%s", ms_b32_unit(byte));
		}
	}
	return outstr;
}
/*
当数据连接变化5才，才认为数据类型发生了改变
pTypePre,pTypeCur:之前和现在的数据类型
pCounter:数据类型连续改变的计数
dataName:数据通道/标识名称
dateTypePre,dateTypeCur:之前和现在的数据类型名称
*/
ms_bool mst_api_isDatatypeChange( ms_s08 *pTypePre, ms_s08 *pTypeCur,ms_byte * pCounter,
	ms_string dataName,ms_string dateTypePre,ms_string dateTypeCur)
{
	if((*pTypePre)!=(*pTypeCur)){
		if((++(*pCounter))>5){
			(*pCounter)=0;
			ms_waring("[%s] data change(%d->%d)  from  %s to %s ",dataName,(*pTypePre),(*pTypeCur),dateTypePre,dateTypeCur);
			return ms_true;
		}else{
			//ms_waring("[%s] data maybe change(%d->%d)  from  %s to %s,maybe judge error(times:%d)",dataName,(*ptypebf),(*ptypeaf),dateTypePre,dateTypeCur,(*pdatatype_times));
			(*pTypeCur)=(*pTypePre);
		}
	}else{
		(*pCounter)=0;
	}
	return ms_false;
}
ms_void mscommon_api_info(ms_string ms_out pbuf)
{
#if defined OS_LINUX_SOC  
	#if defined OS_LINUX_X64 
		strcat(pbuf, "linux64");
	#endif
	#if defined OS_LINUX_X32 
		strcat(pbuf, "linux32");
	#endif
#else
	strcat(pbuf, "unlinux_soc");
#endif
	strcat(pbuf, "|json|env|epoll|fifo|memapi|netframe|net|system|mpegts|signal|thread|time|types");
}
ms_string mscommon_api_version(ms_void)
{
	return "01.04.23";
}
//res
#endif
